<?php

namespace app\api\controller\audit;

use app\api\controller\Api;
use app\common\audit\logic\AuditConfigLogic;
use app\common\audit\model\AuditConfig;
use app\common\audit\validate\AuditConfigValidate;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\db\exception\PDOException;
use util\Format;

class Config extends Api
{
    // 审核配置列表
    public function index()
    {
        $post = $this->request->post();

        list($cfg, $limit) = Format::pagination($post);

        $where = [];
        if (!empty($post['company_id'])) $where['company_id'] = $post['company_id'];
        if (!empty($post['timing'])) $where['timing'] = $post['timing'];

        $model = new AuditConfig();
        $pg = $model->where($where)->order('sort desc,id')->paginate($limit);
        $list = $pg->getCollection();
        foreach ($list as $row) {
            $row->append(['company', 'tag']);
        }

        $result['list'] = $list;
        $result['pagination'] = Format::pgResult($pg);

        return success($result, '获取数据成功');
    }

    public function read()
    {
        $post = $this->request->post();
        if (empty($post['id'])) return error([], "参数错误");

        $model = new AuditConfig();

        try {
            $row = $model->where('id', $post['id'])->find();
            $row->append(['company', 'tag']);

            $result['row'] = $row;
            return success($result);
        } catch (DataNotFoundException $e) {

            return error([], "该配置已不存在");
        }
    }

    // 创建审核配置项
    public function create()
    {
        $post = $this->request->post();
        // 验证表单
        $validate = new AuditConfigValidate();
        if (!$validate->scene('create')->check($post)) return error([], $validate->getError());

        $model = new AuditConfig();
        try {
            $rs = $model->save($post);
        } catch (PDOException $e) {
            return error([], $e->getMessage());
        }

        if ($rs) {
            $logic = new AuditConfigLogic();
            $logic->getAudit($post['company_id'], $post['timing']);
            return success([], '创建成功');
        } else {
            return error([], '创建失败');
        }
    }

    // 编辑审核配置项
    public function save()
    {
        $post = $this->request->post();
        // 验证表单
        $validate = new AuditConfigValidate();
        if (!$validate->scene('edit')->check($post)) return error([], $validate->getError());

        $model = new AuditConfig();
        $row = $model->where('id', $post['id'])->find();
        if (empty($row)) return error([], '该审核项已不存在');

        try {
            $rs = $row->save($post);
        } catch (PDOException $e) {
            return error([], $e->getMessage());
        }

        if ($rs) {
            $logic = new AuditConfigLogic();
            $logic->resetAuditCache($post);
            return success([], "编辑审核基本信息成功");
        } else {
            return error([], "编辑审核基本信息失败");
        }
    }

    // 设置审核流程
    public function update()
    {
        $post = $this->request->post();
        // 验证表单
        $validate = new AuditConfigValidate();
        if (!$validate->scene('update')->check($post)) return error([], $validate->getError());

        $model = new AuditConfig();
        $row = $model->where('id', $post['id'])->find();
        if (empty($row)) return error([], '该审核项已不存在');

        try {
            $rs = $row->save($post);
        } catch (PDOException $e) {
            return error([], $e->getMessage());
        }

        if ($rs) {
            $audit = $row->getData();
            $logic = new AuditConfigLogic();
            $logic->resetAuditCache($audit);
            return success([], '设置审核流程成功');
        } else {
            return error([], '创建失败');
        }
    }

    public function delete()
    {
        $post = $this->request->post();
        // 验证表单
        $validate = new AuditConfigValidate();
        if (!$validate->scene('delete')->check($post)) return error([], $validate->getError());

        $model = new AuditConfig();
        $row = $model->where('id', $post['id'])->find();
        if (empty($row)) return error([], '该审核项已不存在');

        $rs = $row->delete();

        if ($rs) {
            return success([], '删除成功');
        } else {
            return error([], '删除失败');
        }
    }

    public function setProcess()
    {
        $post = $this->request->post();

        // 验证表单
        $validate = new AuditConfigValidate();
        if (!$validate->scene('delete')->check($post)) return error([], $validate->getError());

        $model = new AuditConfig();
        $row = $model->where('id', $post['id'])->find();
        if (empty($row)) return error([], '该审核项已不存在');

        try {
            $rs = $row->save($post);
        } catch (PDOException $e) {
            return error([], $e->getMessage());
        }

        if ($rs) {
            $audit = $row->getData();
            $logic = new AuditConfigLogic();
            $logic->resetAuditCache($audit);
            return success([], '设置审核流程成功');
        } else {
            return error([], '创建失败');
        }
    }
}